// const { get } = require("../../app");



$(function(){
    reLoad()
    //nva 的点击效果
    $(".hotSearch").hide()
    $(".searchHistory").hide()
    var as=document.querySelectorAll("nav div a");

    for(let i=0;i<as.length;i++){
        $("nav div a").eq(i).attr("index",i);
        
    }
    $("nav div a").on("click",function(){
        $("nav div a").prop("class",'')
        $(this).prop("class",'nav_class')
    })
    $("nav div").on("click",function(){
        var index=$(this).children("a").attr("index")
        $("nav div a").prop("class",'')
        $("nav div a").eq(index).prop("class",'nav_class')
        
    })
    $("nav div a").eq(0).on("click",function(){
        $(".nav_1_content").show();
        $(".nav_2_content").hide();
        $(".nav_3_content").hide();
        $(".hotSearch").hide()
        $(".searchHistory").hide()
    })
    $("nav div a").eq(1).on("click",function(){
        $(".nav_1_content").hide();
        $(".nav_2_content").show();
        $(".nav_3_content").hide();
        $(".hotSearch").hide()
        $(".searchHistory").hide()
    })
    $("nav div a").eq(2).on("click",function(){
        $(".nav_1_content").hide();
        $(".nav_2_content").hide();
        $(".nav_3_content").show();
        $(".input_search").val('');
        $(".input_search").select()
        $(".serach_header").hide()
        $(".search_content").hide()
        $(".hotSearch").show()
        $(".searchHistory").show()
    })




    // 搜索模块
    var timer=null;
    $(".input_search").on("input",function(){
        var value=$(this).val();
        if($(this).val()==''){
            $(".serach_header").hide()
            $(".search_content").hide()
            $(".hotSearch,.searchHistory").show()
        } else {
            var str='搜索'+'"'+value+'"'
            $(".serach_header").show()
            $(".search_content").show()
            $(".serach_header").text(str)
            $(".hotSearch,.searchHistory").hide()
        }
        
        // 搜索关键字模块
        clearTimeout(timer)
        if($(this).val()!=''){
            timer=setTimeout(function(){
                $.ajax({
                    type:'get',
                    url:'/search/suggest',
                    data:{
                        keywords:value
                    },
                    success:function(e){
                        console.log(e);
                        $(".search_content ul").empty()
                       for(let i=0;i<e.result.songs.length;i++){
                        var html='<div class="search_contetn_icon"><i></i></div><span>'+e.result.songs[i].name+'</span>'
                        var li=document.createElement("li")
                        li.id=e.result.songs[i].id
                        li.innerHTML=html;
                        $(".search_content ul").append(li)
                       }
                    }
                })
            },800)
        }
    })
    //点击热门搜索，发动搜索
    $(".hotSearch .list ").on("click",'li',function(){
        var value= $(this).text();
        $(".input_search").val(value);
        $(".input_search").blur();

        $(".hotSearch").hide()
        $(".searchHistory").hide()
        
        $.ajax({
            type:'get',
            url:'/search',
            data:{
                keywords:value
            },
            success:function(e){
                // console.log(e);
                $(".search_more").show()
                $(".search_more").empty()
                //最佳匹配
                $.ajax({
                    type:'get',
                    url:'/search/multimatch',
                    data:{
                        keywords:value
                    },
                    success:function(e){
                        console.log(e);
                        console.log(1);
                        if(e.result.artist.length>0){
                            var ss=''
                            if(e.result.artist[0].alias.length>0){
                                var ss='('+e.result.artist[0].alias[0]+')'
                            }
                            var html=`
                            <div class="best_search" >
                                <h3>最佳匹配</h3>
                                <a href="#" class="best_search_li">
                                    <div class="best_search_li_img">
                                        <img src="${e.result.artist[0].img1v1Url}" >
                                    </div>
                                    <div class="best_search_li_detail" style="">
                                        <span class="best_search_li_detail_main" >歌手:<span class="best_search_li_atr" > ${e.result.artist[0].name}</span><span class="best_search_li_atr_enname">${ss}</span></span>
                                        <i class="best_search_li_detail_icon"></i>
                                    </div>
                                </a>
                            `
                            var div=document.createElement("div");
                            div.className='search_more';
                            div.innerHTML=html;
                            $(".search_more").prepend(div)
                        }
                         if(e.result.album.length>0){
                                var html2=`
                            
                                <div class="best_search_li_img_zj">
                                    <img src="${e.result.album[0].picUrl}">
                                </div>
                                <div class="best_search_li_zj">
                                    <span class="best_search_li_zj_main">专辑: ${e.result.album[0].name}</span>
                                    <span class="best_search_li_zj_main_art">${e.result.album[0].artist.name}</span>
                                    <i class="best_search_li_detail_icon"></i>
                                </div>
                            `
                            var a=document.createElement("a");
                            a.className='best_search_li';
                            a.innerHTML=html2;
                            a.href='#';
                            $(".best_search").append(a);
                            }
                    }
                })
               
                for(let i=0;i<e.result.songs.length;i++){
                    var at='';
                    if(e.result.songs[i].artists.length>1){
                        $.each(e.result.songs[i].artists,function(index,value){
                            at+=value.name+'/'
                        })
                    } else {
                        at=e.result.songs[i].artists[0].name
                    }
                    var html='<div class="newmusic_li_box_box1"><div class="newmusic_li_box_box1_line1">'+e.result.songs[i].name+'</div><div class="newmusic_li_box_box1_line2"><i class="u-hmsprt sghot"></i>'+at+' - '+e.result.songs[i].album.name+'</div></div><div class="newmusic_li_box_box2"><span class="u-hmsprt sgchply"></span></div>'
    
                    var a=document.createElement("a");
                    a.className='newmusic_content_li';
                    a.innerHTML=html;
                    a.id=e.result.songs[i].id;
                    a.href='singsong.html?id='+e.result.songs[i].id;
                    if(e.result.songs[i].copyrightId==0){
                        a.children[0].children[1].children[0].className=''
                    }
                    
                    $(".search_more").append(a);
                    // console.log($(".search_more"));
                }
            }
        })


    })
     //点击搜索记录，发动搜索
     $(".searchHistory ul").on("click",'li',function(){
        var value= $(this).text();
        $(".input_search").val(value);
        $(".input_search").blur();

        $(".hotSearch").hide()
        $(".searchHistory").hide()

        $.ajax({
            type:'get',
            url:'/search',
            data:{
                keywords:value
            },
            success:function(e){
                console.log(e);
                $(".search_more").show()
                $(".search_more").empty()
                //最佳匹配
                $.ajax({
                    type:'get',
                    url:'/search/multimatch',
                    data:{
                        keywords:value
                    },
                    success:function(e){
                        console.log(e);
                        console.log(1);
                        if(e.result.artist.length>0){
                            var ss=''
                            if(e.result.artist[0].alias.length>0){
                                var ss='('+e.result.artist[0].alias[0]+')'
                            }
                            var html=`
                            <div class="best_search" >
                                <h3>最佳匹配</h3>
                                <a href="#" class="best_search_li">
                                    <div class="best_search_li_img">
                                        <img src="${e.result.artist[0].img1v1Url}" >
                                    </div>
                                    <div class="best_search_li_detail" style="">
                                        <span class="best_search_li_detail_main" >歌手:<span class="best_search_li_atr" > ${e.result.artist[0].name}</span><span class="best_search_li_atr_enname">${ss}</span></span>
                                        <i class="best_search_li_detail_icon"></i>
                                    </div>
                                </a>
                            `
                            var div=document.createElement("div");
                            div.className='search_more';
                            div.innerHTML=html;
                            $(".search_more").prepend(div)
                        }
                         if(e.result.album.length>0){
                                var html2=`
                            
                                <div class="best_search_li_img_zj">
                                    <img src="${e.result.album[0].picUrl}">
                                </div>
                                <div class="best_search_li_zj">
                                    <span class="best_search_li_zj_main">专辑: ${e.result.album[0].name}</span>
                                    <span class="best_search_li_zj_main_art">${e.result.album[0].artist.name}</span>
                                    <i class="best_search_li_detail_icon"></i>
                                </div>
                            `
                            var a=document.createElement("a");
                            a.className='best_search_li';
                            a.innerHTML=html2;
                            a.href='#';
                            $(".best_search").append(a);
                            }
                    }
                })
                for(let i=0;i<e.result.songs.length;i++){
                    var at='';
                    if(e.result.songs[i].artists.length>1){
                        $.each(e.result.songs[i].artists,function(index,value){
                            at+=value.name+'/'
                        })
                    } else {
                        at=e.result.songs[i].artists[0].name
                    }
                    var html='<div class="newmusic_li_box_box1"><div class="newmusic_li_box_box1_line1">'+e.result.songs[i].name+'</div><div class="newmusic_li_box_box1_line2"><i class="u-hmsprt sghot"></i>'+at+' - '+e.result.songs[i].album.name+'</div></div><div class="newmusic_li_box_box2"><span class="u-hmsprt sgchply"></span></div>'
    
                    var a=document.createElement("a");
                    a.className='newmusic_content_li';
                    a.innerHTML=html;
                    a.id=e.result.songs[i].id;
                    a.href='singsong.html?id='+e.result.songs[i].id;
                    if(e.result.songs[i].copyrightId==0){
                        a.children[0].children[1].children[0].className=''
                    }
                    
                    $(".search_more").append(a);
                    console.log($(".search_more"));
                }
            }
        })


    })

    // 点击搜索为""，发动搜索
    $(".search_content ul").on("click",'li',function(){
        var value= $(this).text();
        $(".input_search").val(value);
        $(".input_search").blur();

        $(".hotSearch").hide()
        $(".searchHistory").hide()
        $(".serach_header").hide()
        $(".search_content").hide()
        $.ajax({
            type:'get',
            url:'/search',
            data:{
                keywords:value
            },
            success:function(e){
                console.log(e);
                $(".search_more").show()
                $(".search_more").empty()
                //最佳匹配
                $.ajax({
                    type:'get',
                    url:'/search/multimatch',
                    data:{
                        keywords:value
                    },
                    success:function(e){
                        console.log(e);
                        console.log(1);
                        if(e.result.artist.length>0){
                            var ss=''
                            if(e.result.artist[0].alias.length>0){
                                var ss='('+e.result.artist[0].alias[0]+')'
                            }
                            var html=`
                            <div class="best_search" >
                                <h3>最佳匹配</h3>
                                <a href="#" class="best_search_li">
                                    <div class="best_search_li_img">
                                        <img src="${e.result.artist[0].img1v1Url}" >
                                    </div>
                                    <div class="best_search_li_detail" style="">
                                        <span class="best_search_li_detail_main" >歌手:<span class="best_search_li_atr" > ${e.result.artist[0].name}</span><span class="best_search_li_atr_enname">${ss}</span></span>
                                        <i class="best_search_li_detail_icon"></i>
                                    </div>
                                </a>
                            `
                            var div=document.createElement("div");
                            div.className='search_more';
                            div.innerHTML=html;
                            $(".search_more").prepend(div)
                        }
                         if(e.result.album.length>0){
                                var html2=`
                            
                                <div class="best_search_li_img_zj">
                                    <img src="${e.result.album[0].picUrl}">
                                </div>
                                <div class="best_search_li_zj">
                                    <span class="best_search_li_zj_main">专辑: ${e.result.album[0].name}</span>
                                    <span class="best_search_li_zj_main_art">${e.result.album[0].artist.name}</span>
                                    <i class="best_search_li_detail_icon"></i>
                                </div>
                            `
                            var a=document.createElement("a");
                            a.className='best_search_li';
                            a.innerHTML=html2;
                            a.href='#';
                            $(".best_search").append(a);
                            }
                    }
                })
                for(let i=0;i<e.result.songs.length;i++){
                    var at='';
                    if(e.result.songs[i].artists.length>1){
                        $.each(e.result.songs[i].artists,function(index,value){
                            at+=value.name+'/'
                        })
                    } else {
                        at=e.result.songs[i].artists[0].name
                    }
                    var html='<div class="newmusic_li_box_box1"><div class="newmusic_li_box_box1_line1">'+e.result.songs[i].name+'</div><div class="newmusic_li_box_box1_line2"><i class="u-hmsprt sghot"></i>'+at+' - '+e.result.songs[i].album.name+'</div></div><div class="newmusic_li_box_box2"><span class="u-hmsprt sgchply"></span></div>'
    
                    var a=document.createElement("a");
                    a.className='newmusic_content_li';
                    a.innerHTML=html;
                    a.id=e.result.songs[i].id;
                    a.href='singsong.html?id='+e.result.songs[i].id;
                    if(e.result.songs[i].copyrightId==0){
                        a.children[0].children[1].children[0].className=''
                    }
                    
                    $(".search_more").append(a);
                    // console.log($(".search_more"));
                }
            }
        })


    })





    //热门搜索
    $.ajax({
        type:'get',
        url:'/search/hot',
        success:function(e){
            // console.log(e);
            for(let i=0;i<e.result.hots.length;i++){
                var html='<a href="#">'+e.result.hots[i].first+'</a>'
                var li=document.createElement("li");
                li.innerHTML=html;
                $(".hotSearch .list").append(li)
            }
        }
    })




    //歌单
    $.ajax({
        type:'get',
        url:'/personalized',
        success:function(e){  
            // console.log(e);
           for(let i=0;i<6;i++){
            let count=getCountplay(e.result[i].playCount)
            var html='<div class="remd_img"><img class="u-img"src="'+e.result[i].picUrl+'"><span class="edit_content_count">'+count +'</span></div><p class="edit_content_text">'+e.result[i].name+'</p>'
            var a=document.createElement("a");
            a.className='edit_content_li'
            a.innerHTML=html;
            a.id=e.result[i].id;
            a.href='sonlist.html?id='+e.result[i].id
            $(".edit_content").append(a)
           }
        }
    })
    // 最新音乐
    $.ajax({
        type:'get',
        url:'/personalized/newsong',
        success:function(e){
            // console.log(e);
            for(let i=0;i<e.result.length;i++){
                var at='';
                if(e.result[i].song.artists.length>1){
                    $.each(e.result[i].song.artists,function(index,value){
                        at+=value.name+'/'
                    })
                } else {
                    at=e.result[i].song.artists[0].name
                }
                var html='<div class="newmusic_li_box_box1"><div class="newmusic_li_box_box1_line1">'+e.result[i].name+'</div><div class="newmusic_li_box_box1_line2"><i class="u-hmsprt sghot"></i>'+at+' - '+e.result[i].song.album.name+'</div></div><div class="newmusic_li_box_box2"><span class="u-hmsprt sgchply"></span></div>'

                var a=document.createElement("a");
                a.className='newmusic_content_li';
                a.innerHTML=html;
                a.id=e.result[i].id;
                a.href='singsong.html?id='+e.result[i].id;
                if(e.result[i].song.copyright==1){
                    a.children[0].children[1].children[0].className=''
                }
                $(".newmusic_content").append(a);
            }
        }
    })

    // 热歌榜的歌单
//   获取所有歌单
    // $.ajax({
    //     type:'get',
    //     url:'/toplist/detail',
    //     success:function(e){
    //         console.log(e);
    //         var idx=e.list[3].id;
    //         console.log(idx);
    //     }
    // })
    $.ajax({
        type:'get',
        url:'/top/list?idx=1',
        success:function(e){
            
            var song=e.playlist.tracks
            console.log(song);
            for(let i=0;i<20;i++){
                var num=i+1;
                num=num>=10? num:'0'+num;

                var de=song[i].alia;
                if(de.length>1){
                    var arr='';
                    $.each(de,function(index,value){
                        arr+=value+'/'
                     })
                     arr='('+arr+')'
                } else if(de.length>0) {
                    arr='('+de[0]+')'
                } else {
                    arr=''
                }

                var art=song[i].ar;
                var arts=''
                $.each(art,function(index,value){
                    if(index==art.length-1){
                        arts+=value.name
                    } else {
                        arts+=value.name+'/'
                    }
                })

                var html='<div class="nav2_li1" style="color: #df3436;">'+num+'</div><div class="nav_2-3"><div class="nav2_li2"><div class="nav2_li2_line1">'+song[i].name+arr+'</div><div class="nav2_li2_line2"><i class="u-hmsprt sghot"></i>'+arts+' -'+song[i].al.name+'</div></div><div class="nav2_li3"><span class="u-hmsprt sgchply"></span></div></div>'
                var a=document.createElement("a");
                a.innerHTML=html;
                a.className='nav_2_content_content_li';
                a.href='singsong.html?id='+song[i].id
                a.id=song[i].id;
                if(i>2){
                    a.children[0].style.color=''
                }
                // if(song[i].copyright==1){
                //     a.children[1].children[0].children[1].children[0].className=''
                //     console.log(1);
                // }
                $(".nav_2_content_content_list").append(a);
            }
            var html1='<p style="color: #999;font-size: 14px;">查看完整歌单 > </p>'
            var a=document.createElement("a");
            a.innerHTML=html1
            a.className='nav_2_content_content_li_last';
            a.href='https://m.music.163.com/m/applink/?scheme=orpheus%3A%2F%2Fplaylist%2F3778678'
            $(".nav_2_content_content_list").append(a);
        }
    })






    // 搜索模块本地存储
    $(".input_search").on("keyup",function(e){
        if(e.keyCode==13){
            var value=$(this).val()
            if(value!=''){
                var data=getData();
                console.log(data);
                data.push({title:value})
                saveData(data)
                reLoad()
                
                $(".input_search").blur()
                $(".serach_header").hide()
                $(".search_content").hide()
                //搜索多重匹配
                // $.ajax({
                //     type:'get',
                //     url:'/search',
                //     data:{
                //         keywords:value
                //     },
                //     success:function(e){
                //         console.log(e);
                //     }
                // })
                $.ajax({
                    type:'get',
                    url:'/search',
                    data:{
                        keywords:value
                    },
                    success:function(e){
                        console.log(e);
                        $(".search_more").show()
                        $(".search_more").empty()
                        //最佳匹配
                $.ajax({
                    type:'get',
                    url:'/search/multimatch',
                    data:{
                        keywords:value
                    },
                    success:function(e){
                        console.log(e);
                        console.log(1);
                        if(e.result.artist.length>0){
                            var ss=''
                            if(e.result.artist[0].alias.length>0){
                                var ss='('+e.result.artist[0].alias[0]+')'
                            }
                            var html=`
                            <div class="best_search" >
                                <h3>最佳匹配</h3>
                                <a href="#" class="best_search_li">
                                    <div class="best_search_li_img">
                                        <img src="${e.result.artist[0].img1v1Url}" >
                                    </div>
                                    <div class="best_search_li_detail" style="">
                                        <span class="best_search_li_detail_main" >歌手:<span class="best_search_li_atr" > ${e.result.artist[0].name}</span><span class="best_search_li_atr_enname">${ss}</span></span>
                                        <i class="best_search_li_detail_icon"></i>
                                    </div>
                                </a>
                            `
                            var div=document.createElement("div");
                            div.className='search_more';
                            div.innerHTML=html;
                            $(".search_more").prepend(div)
                        }
                         if(e.result.album.length>0){
                                var html2=`
                            
                                <div class="best_search_li_img_zj">
                                    <img src="${e.result.album[0].picUrl}">
                                </div>
                                <div class="best_search_li_zj">
                                    <span class="best_search_li_zj_main">专辑: ${e.result.album[0].name}</span>
                                    <span class="best_search_li_zj_main_art">${e.result.album[0].artist.name}</span>
                                    <i class="best_search_li_detail_icon"></i>
                                </div>
                            `
                            var a=document.createElement("a");
                            a.className='best_search_li';
                            a.innerHTML=html2;
                            a.href='#';
                            $(".best_search").append(a);
                            }
                    }
                })
                        for(let i=0;i<e.result.songs.length;i++){
                            var at='';
                            if(e.result.songs[i].artists.length>1){
                                $.each(e.result.songs[i].artists,function(index,value){
                                    at+=value.name+'/'
                                })
                            } else {
                                at=e.result.songs[i].artists[0].name
                            }
                            var html='<div class="newmusic_li_box_box1"><div class="newmusic_li_box_box1_line1">'+e.result.songs[i].name+'</div><div class="newmusic_li_box_box1_line2"><i class="u-hmsprt sghot"></i>'+at+' - '+e.result.songs[i].album.name+'</div></div><div class="newmusic_li_box_box2"><span class="u-hmsprt sgchply"></span></div>'
            
                            var a=document.createElement("a");
                            a.className='newmusic_content_li';
                            a.innerHTML=html;
                            a.id=e.result.songs[i].id;
                            a.href='singsong.html?id='+e.result.songs[i].id;
                            if(e.result.songs[i].copyrightId==0){
                                a.children[0].children[1].children[0].className=''
                            }
                            
                            $(".search_more").append(a);
                            console.log($(".search_more"));
                        }
                    }
                })

            }

            


        }
    })

    $(".input_search").on("focus",function(){
        // alert(11)
        $(".search_more").hide()
        if($(this).val()!=''){
            $(".serach_header").show()
            $(".search_content").show()
        } else {
            $(".hotSearch,.searchHistory").show()
        }
    })

    







    //删除本地存储
    $(".searchHistory ul").on("click",".history_last",function(e){
        e.stopPropagation()
        var index=$(this).attr("index");
        var data=getData();
        data.splice(index,1);
        saveData(data)
        reLoad()
    })

    function getData(){
        var data=localStorage.getItem('search');
        if(data!=null){
            data=JSON.parse(data);
            return data
        } else {
            data=[];
            return data
        }
    }
    function saveData(data){
        localStorage.setItem('search',JSON.stringify(data))
    }
    function reLoad(){
        var data=getData();
        $(".searchHistory ul").empty()
        for(let i=0;i<data.length;i++){
            var html='<div class="time_icon" ><i></i></div><div class="Htext">'+data[i].title+'</div><div class="history_last" index="'+i+'"><i></i></div>'
            var li=document.createElement("li");
            li.innerHTML=html;
            $(".searchHistory ul").prepend(li)
        }
    }




















    //计数功能
    function getCountplay(num){
        if(num>=100000000){
            var one=Math.floor(num/100000000);
            var tow=Math.floor(num%100000000/10000000);
            num=one+'.'+tow+"亿"
            return num
        } else if(num>=10000) {
            var one=Math.floor(num/10000);
            var tow=Math.floor(num%10000/1000);
            num=one+'.'+tow+"万"
            return num
        }
    }

    




    

























})